import { PlusOutlined } from '@ant-design/icons';
import { Button, Divider, message, Modal, Drawer ,Table, Tag, Radio, Space} from 'antd';
import React, {useState, useRef, useEffect} from 'react';
import { PageContainer, FooterToolbar } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import ProDescriptions from '@ant-design/pro-descriptions';
import { queryRule } from './service';
import {history} from "umi";
import request from "@/utils/request";

const TableList = () => {
  const actionRef = useRef();
  const [row, setRow] = useState();
  const [isModalVisible, setIsModalVisible] = useState(false);
  const [delValue, setDelValue] = useState({});
  const [data, dataUpdate] = useState({})
  useEffect(() => {
    request(`/api/index/getTotal`).then(res => {
      if (res.Code != 0) {
        message.error(res.Msg)
      }
      console.log(res)
      if (res.Data) {
        console.log(res.Data)
        dataUpdate(res.Data)
      }
    })
  }, [])
  const handleOk = () => {
    request(`/api/miner/del?MinerId=${delValue.MinerId}`).then(res=>{
      if (res.Code != 0 ){
        message.error(res.Msg)
      }
    })
    setIsModalVisible(false);
    message.success('删除成功！');

    location.reload()
  };

  const handleCancel = () => {
    setIsModalVisible(false);
  };

  const columns = [
    {
      title: '矿工号',
      dataIndex: 'MinerId',
      sorter: (a, b) => a.MinerId > b.MinerId,
    },
    {
      title: '矿池名称',
      dataIndex: 'PoolName',
      sorter: (a, b) => a.PoolName > b.PoolName,
    },
    {
      title: '扇区大小',
      dataIndex: 'Size',
      sorter: (a, b) => a.Size > b.Size,
    },
    {
      title: '矿工余额(Fil)',
      dataIndex: 'Balance',
      sorter: (a, b) => a.Balance - b.Balance,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '有效算力(TiB)',
      dataIndex: 'Power',
      sorter: (a, b) => a.Power - b.Power,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '算力占比(%)',
      dataIndex: 'PowerRatio',
      sorter: (a, b) => a.PowerRatio - b.PowerRatio,
      render: (text) => {
        return text.toFixed(4)
      }
    },
    {
      title: '累计出块数',
      dataIndex: 'RewardCount',
      sorter: (a, b) => a.RewardCount - b.RewardCount,
    },
    {
      title: '累计出块奖励(Fil)',
      dataIndex: 'RewardFil',
      sorter: (a, b) => a.RewardFil - b.RewardFil,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '错误扇区数',
      dataIndex: 'FaultySectors',
      sorter: (a, b) => a.FaultySectors - b.FaultySectors,
    },
    {
      title: '今天密封算力',
      dataIndex: 'TodayPowerGrowth',
      sorter: (a, b) => a.TodayPowerGrowth - b.TodayPowerGrowth,
      render: (text) => {
        return text.toFixed(2)
      }
    },

    {
      title: '今天预估密封算力',
      dataIndex: 'ExpectIncreaseComputingPower',
      sorter: (a, b) => a.ExpectIncreaseComputingPower - b.ExpectIncreaseComputingPower,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '今天幸运值',
      dataIndex: 'TodayLucky',
      sorter: (a, b) => a.TodayLucky - b.TodayLucky,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '今天出块异常',
      dataIndex: 'TodayAbnormalBlock',
      sorter: (a, b) => a.TodayAbnormalBlock - b.TodayAbnormalBlock,
      render: (text,record) => {
        if (!record.DoneRoundWinner ) {
          return "-"
        }
        return text
      },
    },
    {
      title: '今天罚币',
      dataIndex: 'TodayBurnFil',
      sorter: (a, b) => a.TodayBurnFil - b.TodayBurnFil,
      render: (text) => {
        return text.toFixed(2)
      }
    },
  ];

  const request2 = async(
    // 第一个参数 params 查询表单和 params 参数的结合
    // 第一个参数中一定会有 pageSize 和  current ，这两个参数是 antd 的规范
    params ,
    sorter,
    filter,
  ) => {
    // 这里需要返回一个 Promise,在返回之前你可以进行数据转化
    // 如果需要转化参数可以在这里进行修改
    params.MinersMonitor = true
    console.log(params)

    const result = await queryRule({...params,sorter,filter});
    if (result.Code != 0) {
      message.error(result.Msg)
      return
    }
    console.log(result)
    return {
      data:result.Data.Data,
      success:result.Code === 0,
      total:result.Data.Total,
    };
  }


  return (

    <PageContainer>

      <ProTable
        actionRef={actionRef}
        rowKey="MinerId"
        search={false}
        options={false}
        // request={request2}
        dataSource={data.List}
        columns={columns}
        style={{"cursor":"pointer"}}
        onRow={record => {
          return {
            onClick: event => {
              history.push({
                pathname: `/minerInfo/${record.MinerId}`,
              })
            }, // 点击行
            onMouseEnter: event => {},
          };
        }}
      />

      <Drawer
        width={600}
        visible={!!row}
        onClose={() => {
          setRow(undefined);
        }}
        closable={false}
      >
        {row?.name && (
          <ProDescriptions
            column={2}
            title={row?.name}
            request={async () => ({
              data: row || {},
            })}
            params={{
              id: row?.name,
            }}
            columns={columns}
          />
        )}
      </Drawer>
    </PageContainer>
  );
};

export default TableList;
